package com.yangxd.system.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.yangxd.handler.BusinessException;
import com.yangxd.response.Result;
import com.yangxd.response.ResultCode;
import com.yangxd.system.entity.TbUser;
import com.yangxd.system.service.AliOssService;
import com.yangxd.system.service.TbUserService;
import com.yangxd.system.vo.UserVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiModelProperty;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.List;

/**
 * <p>
 * 用户表 前端控制器
 * </p>
 *
 * @author yangxd
 * @since 2021-02-26
 */
@RestController
@RequestMapping("/system/tb-user")
@Api(value="系统用户模块", tags = "系统用户接口")
public class TbUserController {

    @Autowired
    private TbUserService tbUserService;

    @Autowired
    private AliOssService aliOssService;



    /**
     * 查询所有用户信息
     *
     * @return
     */
   /* @GetMapping
    @ApiOperation(value="用户列表", notes = "查询所有用户信息")
    Result findUsers(@RequestParam(required = true,defaultValue = "1" )Integer current,
                     @RequestParam(required = true,defaultValue = "6")Integer size) {
        //对用户进行分页,泛型中注入的就是用户实体类
        Page<TbUser> page = new Page<>(current,size);
        //mybatis-plus封装的方法进行分页查询
        Page<TbUser> userPage = tbUserService.page(page);

        //

        long total = userPage.getTotal();
        List<TbUser> userList = userPage.getRecords();
        if(null != userList && userList.size() > 0) {
            return Result.ok().data("total", total).data("users", userList);
        } else {
            return Result.error();
        }
    }*/

    @PostMapping
    @ApiOperation(value="用户列表", notes = "查询所有用户信息")
    Result findUsers(@RequestParam(required = true,defaultValue = "1" )Integer current,
                     @RequestParam(required = true,defaultValue = "6")Integer size,
                     @RequestBody UserVO userVO) {
        //对用户进行分页,泛型中注入的就是用户实体类
        Page<TbUser> page = new Page<>(current,size);
        QueryWrapper<TbUser> wrapper = getQueryWrapper(userVO);
       IPage<TbUser> userPage = tbUserService.findPageUser(page,wrapper);
        //mybatis-plus封装的方法进行分页查询
        //Page<TbUser> userPage = tbUserService.page(page);

        //

        long total = userPage.getTotal();
        List<TbUser> userList = userPage.getRecords();
        if(null != userList && userList.size() > 0) {
            return Result.ok().data("total", total).data("users", userList);
        } else {
            return Result.error();
        }
    }

    /**
     * 根据id查询某个用户
     *
     * @param id
     * @return
     */
    @GetMapping("/{id}")
    @ApiOperation(value="单个用户", notes = "根据Id查询单个用户信息")
    Result findUserById(@PathVariable("id") Long id) {
        TbUser tbUser = tbUserService.getById(id);
        if(null != tbUser) {
            return Result.ok().data("user",tbUser);
        } else {
            throw new BusinessException(ResultCode.USER_ACCOUNT_NOT_EXIST.getCode(),ResultCode.USER_ACCOUNT_NOT_EXIST.getMessage());
        }
    }

    private QueryWrapper<TbUser> getQueryWrapper(UserVO userVO) {
        QueryWrapper<TbUser> queryWrapper = new QueryWrapper<>();
        if(userVO != null) {
            if(!StringUtils.isEmpty(userVO.getDepartmentId())) {
                queryWrapper.eq("department_id",userVO.getDepartmentId());
            }
            if(!StringUtils.isEmpty(userVO.getUsername())) {
                queryWrapper.eq("username",userVO.getUsername());
            }
            if(!StringUtils.isEmpty(userVO.getNickname())) {
                queryWrapper.eq("nickname",userVO.getNickname());
            }
            if(!StringUtils.isEmpty(userVO.getEmail())) {
                queryWrapper.eq("email",userVO.getEmail());
            }
            if(!StringUtils.isEmpty(userVO.getSex())) {
                queryWrapper.eq("sex",userVO.getSex());
            }
        }
        return queryWrapper;

    }

    @ApiOperation(value = "添加用户",notes = "添加用户信息")
    @PostMapping("/addUser")
    public Result addUser(@RequestBody TbUser tbUser) {
        try {
            tbUserService.addUser(tbUser);
            return Result.ok();

        }catch (Exception e) {
            return Result.error().message("增加失败！");
        }


    }



}

